<template>
  <HomePanel ref="target" title="人气推荐" sub-title="人气爆款 不容错过">
    <ul v-if="goods.length > 0" class="goods-list">
      <li v-for="item in goods" :key="item.id">
        <RouterLink to="/">
          <!-- <img :src="item.picture" alt="" /> -->
          <img v-imgLazy="item.picture" alt="" />
          <p class="name">{{ item.title }}</p>
          <p class="desc">{{ item.alt }}</p>
        </RouterLink>
      </li>
    </ul>
    <ul v-else class="goods-list">
      <li v-for="item in 4" :key="item">
        <XtxSkeleton :width="306" :height="406" />
      </li>
    </ul>
  </HomePanel>
</template>

<script>
import HomePanel from './home-panel'
import { reqFindHot } from '@/api/home'
import { useObserver } from '@/compositions'
import { ref } from 'vue'
// import { useIntersectionObserver } from '@vueuse/core'
export default {
  name: 'HomeHot',
  components: { HomePanel },
  setup () {
    const goods = ref([])
    async function findHot () {
      const res = await reqFindHot()
      goods.value = res.result
    }

    // const target = ref(null)
    // const { stop } = useIntersectionObserver(
    //   target,
    //   ([{ isIntersecting }], observerElement) => {
    //     // 进入可视区域了
    //     if (isIntersecting) {
    //       // 停止监听
    //       stop()
    //       findHot()
    //     }
    //   }
    // )

    const target = useObserver(findHot)

    return {
      goods,
      target
    }
  }
}
</script>

<style scoped lang="less">
.goods-list {
  display: flex;
  justify-content: space-between;
  height: 426px;
  li {
    width: 306px;
    height: 406px;
    img {
      width: 306px;
      height: 306px;
    }
    p {
      font-size: 22px;
      padding-top: 12px;
      text-align: center;
    }
    .desc {
      color: #999;
      font-size: 18px;
    }
  }
}
</style>
